<template>
	<div class="div-container">
		<Card :bordered="true">
			<p slot="title">租户注册</p>
			<Form ref="formInfo" :model="info" :rules="infoRules" @submit.native.prevent>
				<FormItem prop="loginName">
					<Input type="text" v-model="info.loginName" placeholder="用户名" />
				</FormItem>
				<FormItem prop="password">
					<Input type="password" v-model="info.password" placeholder="密码" />
				</FormItem>
				<FormItem prop="confirmPassword">
					<Input type="password" v-model="info.confirmPassword" placeholder="确认密码" />
				</FormItem>
				<FormItem prop="orgCode">
					<Input type="text" v-model="info.orgCode" placeholder="公司代码" />
				</FormItem>
				<FormItem prop="orgName">
					<Input type="text" v-model="info.orgName" placeholder="公司全称" />
				</FormItem>
				<FormItem prop="orgAliasName">
					<Input type="text" v-model="info.orgAliasName" placeholder="公司简称" />
				</FormItem>
				<FormItem>
					<Button type="primary" @click="handleRegister">提交</Button>
					<Button style="margin-left:5px;" @click="cancelRegister">取消</Button>
				</FormItem>
			</Form>
		</Card>
	</div>
</template>

<script language="JavaScript">
	export default {
		components: {},
		data() {
			const validateConfirmPass = (rule, value, callback) => {
				if (!value) {
					callback(new Error('必填'))
				} else {
					if (this.info.password !== this.info.confirmPassword) {
						callback(new Error('两次输入不一致'))
					} else {
						callback()
					}
				}
			};
			return {
				info: {},
				infoRules: {
					loginName: [{
						required: true,
						message: '必填'
					}],
					password: [{
						required: true,
						message: '必填'
					}],
					confirmPassword: [{
						validator: validateConfirmPass
					}],
					orgCode: [{
						required: true,
						message: '必填'
					}],
					orgName: [{
						required: true,
						message: '必填'
					}],
					orgAliasName: [{
						required: true,
						message: '必填'
					}],
				}
			}
		},
		computed: {},
		watch: {
			'info.orgCode': function() {
				this.info.orgCode = this.info.orgCode.toUpperCase()
			}
		},
		created: function() {},
		mounted: function() {},
		methods: {
			handleRegister: function() {
				this.$refs['formInfo'].validate((valid) => {
					if (valid) {
						this.$request.post('/sys/user/register', this.info, res => {
							this.$Modal.success({
								title: '注册成功',
								onOk: () => {
									this.$router.replace({
										name: 'login'
									})
								}
							})
						}, false, true)
					}
				})
			},
			// 取消注册
			cancelRegister: function() {
				this.$router.push({
					name: 'login'
				})
			}
		}
	}
</script>

<style lang="scss" scoped="scoped">
	.div-container {
		width: 100%;
		height: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		background: #515a6e;

		.ivu-card {
			width: 400px;
			height: auto;
			background-color: white;
		}
	}
</style>
